import { init, RematchRootState, RematchDispatch } from '@rematch/core';
import loadingPlugin, { ExtraModelsFromLoading } from '@rematch/loading';
import { RootModel } from './rootModel';
import models from './loader';

type FullModel = ExtraModelsFromLoading<RootModel>;

interface ILoadingPlugin {
  loading: {
    models: RematchRootState<RootModel>;
    effects: RootDispatch;
  };
}

const configureStore = () => {
  const store = init<RootModel, FullModel>({
    plugins: [loadingPlugin()],
    models: models as RootModel,
  });
  return store;
};

const store = configureStore();

export type Store = RematchRootState<RootModel> & ILoadingPlugin;
export type RootState = RematchRootState<RootModel, FullModel>;
export type RootDispatch = RematchDispatch<RootModel>;

export default store;
